package com.dq.controller.op.opmerchant.message;

import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.vo.Vo;
import com.dq.domain.message.oneself.Message;
import com.dq.domain.message.oneself.NoticeMessage;
import com.dq.domain.message.oneself.SubscribeMessage;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author cf
 */
@Api(description = "门店发布 通知消息")
@RestController
@RequestMapping("sys/org/noticeMessage/")
public class OrgNoticeMessageController extends AllService {
    /** --------------------------------------------------- 通知消息 --------------------------------------------------- **/

    @ApiOperation("消息批量/单个添加")
    @PostMapping("/save")
    @RequiresPermissions("sys:noticeMessage:save")
    public Vo list(@ApiParam("caseUserIds: 消息接受者 id集合") @RequestBody NoticeMessage noticeMessage) throws AdminException {
        // 发送给顾客
        noticeMessage.setUserType(SubscribeMessage.UserType.client);
        noticeMessageService.saveList(noticeMessage, noticeMessage.getCaseUserIds(), Message.MessageType.wxSystem);

        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:noticeMessage:update")
    public Vo update(@RequestBody @ApiParam(value = "caseUserIds: 员工/顾客 id集合") NoticeMessage noticeMessage) throws AdminException {
        if(noticeMessage.getId() == null) {
            throw new AdminException("缺参id");
        }

        NoticeMessage orgPositionQuery = dao.get(NoticeMessage.class, noticeMessage.getId());
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }
        BeanUtils.copyProperties(noticeMessage, orgPositionQuery, BeanNullUtil.getNullPropertyNames(noticeMessage));
        noticeMessageService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @SysLog("删除")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("sys:noticeMessage:delete")
    public Vo delete(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        NoticeMessage orgPositionQuery = dao.get(NoticeMessage.class, id);
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }

        orgPositionQuery.setDeleted(true);
        noticeMessageService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }
    /** --------------------------------------------------- 通知消息 --------------------------------------------------- **/
}
